package com.ruoyi.info.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.info.mapper.CmsInfoMapper;
import com.ruoyi.info.domain.CmsInfo;
import com.ruoyi.info.service.ICmsInfoService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.ui.ModelMap;

/**
 * 各种信息配置Service业务层处理
 * 
 * @author xwj
 * @date 2023-07-06
 */
@Service
public class CmsInfoServiceImpl implements ICmsInfoService
{
    @Autowired
    private CmsInfoMapper cmsInfoMapper;

    /**
     * 查询各种信息配置
     * 
     * @param id 各种信息配置主键
     * @return 各种信息配置
     */
    @Override
    public CmsInfo selectCmsInfoById(Long id)
    {
        return cmsInfoMapper.selectCmsInfoById(id);
    }

    /**
     * 查询各种信息配置列表
     * 
     * @param cmsInfo 各种信息配置
     * @return 各种信息配置
     */
    @Override
    public List<CmsInfo> selectCmsInfoList(CmsInfo cmsInfo)
    {
        return cmsInfoMapper.selectCmsInfoList(cmsInfo);
    }

    /**
     * 新增各种信息配置
     * 
     * @param cmsInfo 各种信息配置
     * @return 结果
     */
    @Override
    public int insertCmsInfo(CmsInfo cmsInfo)
    {
        cmsInfo.setCreateTime(DateUtils.getNowDate());
        return cmsInfoMapper.insertCmsInfo(cmsInfo);
    }

    /**
     * 修改各种信息配置
     * 
     * @param cmsInfo 各种信息配置
     * @return 结果
     */
    @Override
    public int updateCmsInfo(CmsInfo cmsInfo)
    {
        cmsInfo.setUpdateTime(DateUtils.getNowDate());
        return cmsInfoMapper.updateCmsInfo(cmsInfo);
    }

    /**
     * 批量删除各种信息配置
     * 
     * @param ids 需要删除的各种信息配置主键
     * @return 结果
     */
    @Override
    public int deleteCmsInfoByIds(String ids)
    {
        return cmsInfoMapper.deleteCmsInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除各种信息配置信息
     * 
     * @param id 各种信息配置主键
     * @return 结果
     */
    @Override
    public int deleteCmsInfoById(Long id)
    {
        return cmsInfoMapper.deleteCmsInfoById(id);
    }

    @Override
    public CmsInfo getByKey(String key) {
        CmsInfo param = new CmsInfo();
        param.setKeyCode(key);
        List<CmsInfo> list = cmsInfoMapper.selectCmsInfoList(param);

        if(list != null && list.size() > 0) {
            return list.get(0);
        }

        return null;
    }

    @Override
    public CmsInfo getByKeyCache(String key) {
        CmsInfo entity = null;

        CmsInfo param = new CmsInfo();
        param.setKeyCode(key);
        List<CmsInfo> list = cmsInfoMapper.selectCmsInfoList(param);

        if(list != null && list.size() > 0) {
            entity = list.get(0);
        }
        return entity;
    }

    @Override
    public List<CmsInfo> getCmsInfoListBykeyCache(String key) {
        List<CmsInfo> list = Lists.newArrayList();

        CmsInfo param = new CmsInfo();
        param.setKeyCode(key);
        list = cmsInfoMapper.selectCmsInfoList(param);
        return list;
    }

    @Override
    public void updateBykey(String keyCode, CmsInfo info) {
        CmsInfo param = new CmsInfo();
        param.setKeyCode(keyCode);

        CmsInfo entity = null;

        List<CmsInfo> list = cmsInfoMapper.selectCmsInfoList(param);
        if(list != null && list.size() > 0 ) {
            entity = list.get(0);
        }
        if(entity == null) {
            insertCmsInfo(info);
        } else {
            entity.setValue(info.getValue());
            entity.setImgUrl(info.getImgUrl());
            entity.setExtra(info.getExtra());
            entity.setExt1(info.getExt1());
            entity.setExt2(info.getExt2());

            updateCmsInfo(entity);
        }
    }

    @Override
    public void updateValueBykey(String keyCode, String value) {
        CmsInfo param = new CmsInfo();
        param.setKeyCode(keyCode);

        CmsInfo entity = null;

        List<CmsInfo> list = cmsInfoMapper.selectCmsInfoList(param);
        if(list != null && list.size() > 0 ) {
            entity = list.get(0);
        }
        if(entity == null) {
            entity = new CmsInfo();
            entity.setKeyCode(keyCode);
            entity.setValue(value);

            insertCmsInfo(entity);
        } else {
            entity.setValue(value);
            updateCmsInfo(entity);
        }
    }

    @Override
    public void updateExtraBykey(String keyCode, String extra) {
        CmsInfo param = new CmsInfo();
        param.setKeyCode(keyCode);

        CmsInfo entity = null;

        List<CmsInfo> list = cmsInfoMapper.selectCmsInfoList(param);
        if(list != null && list.size() > 0 ) {
            entity = list.get(0);
        }
        if(entity == null) {
            entity = new CmsInfo();
            entity.setKeyCode(keyCode);
            entity.setExtra(extra);

            insertCmsInfo(entity);
        } else {
            entity.setExtra(extra);
            updateCmsInfo(entity);
        }
    }




}
